﻿unit NewScanEventFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  dxRibbonForm,
  cxGridExportLink,cxDataUtils,
  FIBQuery, pFIBCacheQueries,
  Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  dxSkinsCore, dxSkinMcSkin, dxSkinOffice2010Blue, dxSkinOffice2010Silver,
  dxSkinSeven, dxSkinscxPCPainter, Menus, dxLayoutControl, cxStyles,
  cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxGridLevel, cxClasses, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, cxSplitter, cxContainer,
  cxCheckListBox, StdCtrls, cxButtons, dxSkinsdxRibbonPainter,
  dxLayoutLookAndFeels, dxRibbon, dxLayoutcxEditAdapters, cxTextEdit,
  cxMaskEdit, cxDropDownEdit, cxCalendar, cxCheckBox, cxMemo, FIBDataSet,
  pFIBDataSet, cxPropertiesStore, cxGridCustomPopupMenu, cxGridPopupMenu,
  dxSkinsdxBarPainter, dxmdaset, ImgList, dxBar, cxBarEditItem,
  dxSkinOffice2010Black, dxsbar;

type
  TNewScanEventForm = class(Tdxcustomribbonform)
    lcntrlcontr122: TdxLayoutControl;
    btn1: TcxButton;
    lst1: TcxCheckListBox;
    lst2: TcxCheckListBox;
    btn2: TcxButton;
    btn3: TcxButton;
    btn4: TcxButton;
    btn5: TcxButton;
    lgrp1: TdxLayoutGroup;
    lgrpGrp: TdxLayoutGroup;
    lgrplcntrlcontr122Group2: TdxLayoutGroup;
    lItemlcntrlcontr122Item12: TdxLayoutItem;
    lItemlcntrlcontr122Item14: TdxLayoutItem;
    lItemlcntrlcontr122Item1: TdxLayoutItem;
    dxlytspltrtmlcntrlcontr122SplitterItem1: TdxLayoutSplitterItem;
    lgrp_coll: TdxLayoutGroup;
    lgrplcntrlcontr122Group1: TdxLayoutGroup;
    lItemlcntrlcontr122Item13: TdxLayoutItem;
    lItemlcntrlcontr122Item15: TdxLayoutItem;
    lItemlcntrlcontr122Item11: TdxLayoutItem;
    dxlytspltrtmlcntrlcontr122SplitterItem2: TdxLayoutSplitterItem;
    lItem1: TdxLayoutItem;
    cxspltr1: TcxSplitter;
    lgrp2: TdxLayoutGroup;
    dxLayoutControl1: TdxLayoutControl;
    v1: TcxGridDBTableView;
    l1: TcxGridLevel;
    grd1: TcxGrid;
    ItemGrig: TdxLayoutItem;
    dxlytspltrtm: TdxLayoutSplitterItem;
    v2: TcxGridDBTableView;
    l2: TcxGridLevel;
    grd2: TcxGrid;
    lItemGrid2: TdxLayoutItem;
    rt: TdxRibbonTab;
    dxRibbon1: TdxRibbon;
    LayoutLookAndFeelList1: TdxLayoutLookAndFeelList;
    LayoutCxLookAndFeel1: TdxLayoutCxLookAndFeel;
    lgrp: TdxLayoutGroup;
    dt1: TcxDateEdit;
    Itemd1: TdxLayoutItem;
    dt2: TcxDateEdit;
    itemdt2: TdxLayoutItem;
    v3: TcxGridDBTableView;
    l3: TcxGridLevel;
    grd3: TcxGrid;
    lItemGrid3: TdxLayoutItem;
    ch1: TcxCheckBox;
    lItemCheck: TdxLayoutItem;
    mmo1: TcxMemo;
    lItemMmo: TdxLayoutItem;
    sep3: TdxLayoutSeparatorItem;
    mmo2: TcxMemo;
    lItemlMmo2: TdxLayoutItem;
    ds1: TDataSource;
    UDS: TpFIBDataSet;
    clCode: TcxGridDBColumn;
    clName: TcxGridDBColumn;
    DataSource: TDataSource;
    DS: TpFIBDataSet;
    DSECODE: TFIBStringField;
    DSACCCODE: TFIBStringField;
    DSDACTIVE: TFIBDateField;
    DSTACTIVE: TFIBTimeField;
    DSMASTER: TFIBStringField;
    DSADDR: TFIBStringField;
    DSADDRNAME: TFIBStringField;
    DSETYPE: TFIBStringField;
    DSENAME: TFIBStringField;
    DSUSR: TFIBIntegerField;
    DSUSRNAME: TFIBStringField;
    DSDERECT: TFIBSmallIntField;
    DSSHB: TFIBStringField;
    DSNOTE: TFIBStringField;
    clv1ECODE: TcxGridDBColumn;
    clv1ACCCODE: TcxGridDBColumn;
    clv1DACTIVE: TcxGridDBColumn;
    clv1TACTIVE: TcxGridDBColumn;
    clv1MASTER: TcxGridDBColumn;
    clv1ADDR: TcxGridDBColumn;
    clv1ADDRNAME: TcxGridDBColumn;
    clv1ETYPE: TcxGridDBColumn;
    clv1ENAME: TcxGridDBColumn;
    clv1USR: TcxGridDBColumn;
    clv1USRNAME: TcxGridDBColumn;
    clv1DERECT: TcxGridDBColumn;
    clv1SHB: TcxGridDBColumn;
    clv1NOTE: TcxGridDBColumn;
    CDS: TpFIBDataSet;
    CDSTP: TFIBSmallIntField;
    CDSD: TFIBDateField;
    CDST: TFIBTimeField;
    CDSNAME: TFIBStringField;
    CDSPSUM: TFIBBCDField;
    CDSRSUM: TFIBBCDField;
    CDSSALDO: TFIBBCDField;
    CDSANSWER: TFIBStringField;
    CDSSTATE: TFIBSmallIntField;
    CDSETYPE: TFIBStringField;
    CDSRTYPE: TFIBStringField;
    CDSNOTE: TFIBStringField;
    CDSSDATE: TFIBDateTimeField;
    CDSEDATE: TFIBDateTimeField;
    CDSECODE: TFIBStringField;
    CDSACODE: TFIBStringField;
    CSource: TDataSource;
    clv2TP: TcxGridDBColumn;
    clv2D: TcxGridDBColumn;
    clv2T: TcxGridDBColumn;
    clv2NAME: TcxGridDBColumn;
    clv2PSUM: TcxGridDBColumn;
    clv2RSUM: TcxGridDBColumn;
    clv2SALDO: TcxGridDBColumn;
    clv2ANSWER: TcxGridDBColumn;
    clv2STATE: TcxGridDBColumn;
    clv2ETYPE: TcxGridDBColumn;
    clv2RTYPE: TcxGridDBColumn;
    clv2NOTE: TcxGridDBColumn;
    clv2SDATE: TcxGridDBColumn;
    clv2EDATE: TcxGridDBColumn;
    clv2ECODE: TcxGridDBColumn;
    clv2ACODE: TcxGridDBColumn;
    pm1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    clKONTRAGENT: TcxGridDBColumn;
    clKONTRAGENT_Name: TcxGridDBColumn;
    prop1: TcxPropertiesStore;
    DSKONTRAGENT: TStringField;
    DSKONTRAGENT_NAME: TStringField;
    pmn1: TcxGridPopupMenu;
    grp_srv: TdxLayoutGroup;
    bm1: TdxBarManager;
    bar_srv: TdxBar;
    doc1: TdxBarDockControl;
    Item_doc: TdxLayoutItem;
    btn_CheckAll: TdxBarButton;
    il1: TImageList;
    btn_UnCheckAll: TdxBarButton;
    btn_SaveSrv: TdxBarButton;
    btn_OpenSrv: TdxBarButton;
    v4: TcxGridDBTableView;
    l4: TcxGridLevel;
    grd4: TcxGrid;
    tem_grd4: TdxLayoutItem;
    tbl_srv: TdxMemData;
    ds_srv: TDataSource;
    ds2: TpFIBDataSet;
    clv4Flag: TcxGridDBColumn;
    clv4Srv: TcxGridDBColumn;
    clv4NAME: TcxGridDBColumn;
    clv1SRV: TcxGridDBColumn;
    StdFlt: TcxBarEditItem;
    DSSRV: TStringField;
    grp3: TdxLayoutGroup;
    btn_move: TcxButton;
    Item_move: TdxLayoutItem;
    mt1: TdxMemData;
    clv1Flag: TcxGridDBColumn;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    btn_addev: TcxButton;
    Item_evn: TdxLayoutItem;
    ppm1: TdxSideBarPopupMenu;
    N8: TMenuItem;
    N7: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ch1PropertiesChange(Sender: TObject);
    procedure DataSourceDataChange(Sender: TObject; Field: TField);
    procedure CSourceDataChange(Sender: TObject; Field: TField);
    procedure dt1PropertiesChange(Sender: TObject);
    procedure grd1Enter(Sender: TObject);
    procedure v1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure v4KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure v4DblClick(Sender: TObject);
    procedure btn_CheckAllClick(Sender: TObject);
    procedure btn_UnCheckAllClick(Sender: TObject);
    procedure btn_SaveSrvClick(Sender: TObject);
    procedure btn_OpenSrvClick(Sender: TObject);
    procedure StdFltPropertiesChange(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure btn_moveClick(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure ds1DataChange(Sender: TObject; Field: TField);
    procedure v1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure v1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure btn_addevClick(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure N8Click(Sender: TObject);
  private
    { Private declarations }
    procedure MakeUserList;
    procedure SetNote;
    procedure MakeFullList;
    procedure SetUser;
    procedure Reopen;
    function GetPriorCode: string;
    procedure LoadSrv;
    procedure CreateSrvTbl;
    procedure SrvMark(AMark: boolean);
    procedure SrvListSave;
    procedure SrvListLoad;
    function GetSrv :string;
    procedure SetSrvDef;
    procedure CreateTable;
    function GetCode :string;
    procedure SetUsers;
    procedure MakeNewEvent;
    procedure CloseEvents;
  public
    { Public declarations }
  end;

var
  NewScanEventForm: TNewScanEventForm;

implementation

uses DModule, cmUtils, SetENoteFrm, NameFrm, SrvSaveListFrm, NewMPFrm,
  GetMListFrm, GetRListFrm;

{$R *.dfm}

procedure TNewScanEventForm.SrvMark(AMark: boolean);
begin
 ds_srv.DataSet:=nil;
 with tbl_srv do
  begin
   First;
   while not Eof do
    begin
     if FieldByName('Flag').AsBoolean<>AMark then
      begin
       Edit;
       FieldByName('Flag').AsBoolean:=AMark;
       Post;
      end;
     Next;
    end;
   First;
  end;
 ds_srv.DataSet:=tbl_srv;
end;

procedure TNewScanEventForm.StdFltPropertiesChange(Sender: TObject);
var
  Srv :string;
begin
  //получаем список служб
  srv:=GetSrv;
  if Srv<>'' then
  if StdFlt.CurEditValue=true then
  SetSrvList(srv,tbl_srv) else
  SrvMark(true);
end;

procedure TNewScanEventForm.SetSrvDef;
var
  Srv :string;
begin
  //получаем список служб
  srv:=GetSrv;
  if Srv<>'' then
  if StdFlt.CurEditValue=true then
  SetSrvList(srv,tbl_srv);
end;

{-------------------------------------------------------------------------------
 SrvListSave | Сохраняем список услуг
-------------------------------------------------------------------------------}
procedure TNewScanEventForm.SrvListSave;
 var Form: TNameForm;
begin
 Form:=TNameForm.Create(Application);
 Form.SrvList:=GetSrvList(tbl_srv);
 Form.ShowModal;
 Form.Free;
end;

procedure TNewScanEventForm.btn_SaveSrvClick(Sender: TObject);
begin
  SrvListSave;
end;
{-------------------------------------------------------------------------------
  TSrvListLoad|Загружаем список услуг
-------------------------------------------------------------------------------}
procedure TNewScanEventForm.SrvListLoad;
 var Form: TSrvSaveListForm;
     S: string;
begin
 S:='';
 Form:=TSrvSaveListForm.Create(Application);
 if Form.ShowModal=mrOk then
    S:=Form.GetSrvList;
 Form.Free;
 if S<>'' then SetSrvList(S,tbl_srv);
end;

procedure TNewScanEventForm.btn_moveClick(Sender: TObject);
begin
 //ShowMessage(GetCode);
 if UDS.IsEmpty then Exit;
 SetUsers;
end;

function TNewScanEventForm.GetCode :string;
var
  i: Integer;
begin
  Result:='';
  mt1.DisableControls;
  mt1.First;
  for I := 0 to mt1.RecordCount - 1 do
  begin
    if mt1.FieldByName('Flag').AsBoolean=true then
    begin
      Result:=Result+#39+mt1.FieldByName('AccCode').AsString+#39+',';
    end;
    mt1.Next;
  end;
  if Result<>'' then Delete(Result,Length(Result),1);
  mt1.EnableControls;
end;

procedure TNewScanEventForm.SetUsers;
 var SQL,code: string;
begin
 code:=GetCode;
 if (UDS.IsEmpty) or (mt1.IsEmpty) or (code='') then Exit;
 SQL:='UPDATE N_ReestrData SET Usr='+UDS.FN('Code').AsString+
      ' WHERE acCode in ('+code+');';
 dm1.db1.Execute(SQL);
 {SQL:='UPDATE Reestr SET Usr='+UDS.FN('Code').AsString+
      ' WHERE ACCCODE in ('+code+');';
 dm1.db1.Execute(SQL);}
 Reopen;
end;

procedure TNewScanEventForm.btn_OpenSrvClick(Sender: TObject);
begin
  SrvListLoad;
end;
{-------------------------------------------------------------------------------
 btn_CheckAllClick|Отметить все услуги
-------------------------------------------------------------------------------}
procedure TNewScanEventForm.btn_CheckAllClick(Sender: TObject);
begin
  SrvMark(true);
end;
{-------------------------------------------------------------------------------
 btn_UnCheckAllClick|Снять отметку услуг
-------------------------------------------------------------------------------}
procedure TNewScanEventForm.btn_UnCheckAllClick(Sender: TObject);
begin
  SrvMark(false);
end;

procedure TNewScanEventForm.ch1PropertiesChange(Sender: TObject);
begin
  MakeUserList;
end;

procedure TNewScanEventForm.CSourceDataChange(Sender: TObject; Field: TField);
begin
 mmo2.Text:=CDS.FN('Note').AsString;
end;

procedure TNewScanEventForm.DataSourceDataChange(Sender: TObject;
  Field: TField);
begin
  {SetNote;
  MakeUserList;
  MakeFullList;}
  {if mt1.State in [dsEdit,dsInsert] then
     mt1.Post;}
end;

procedure TNewScanEventForm.ds1DataChange(Sender: TObject; Field: TField);
begin
  btn_move.Caption:='Перенести выбранные счета на '+UDS.FN('Name').AsString;
end;

procedure TNewScanEventForm.dt1PropertiesChange(Sender: TObject);
begin
 MakeFullList;
end;

{-------------------------------------------------------------------------------
GetSrv | Получаем список служб принципала в активном гриде
-------------------------------------------------------------------------------}
function TNewScanEventForm.GetSrv :string;
begin
   Result:=GetSelected('Srv',grd1);
end;

procedure TNewScanEventForm.SetNote;
begin
 mmo1.Lines.Text:='';
 if mt1.IsEmpty then Exit;
 mmo1.Lines.Text:=mt1.FieldByName('Note').AsString;
end;

procedure TNewScanEventForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  RemoveBtn(Name);
  Action:=caFree;
end;

procedure TNewScanEventForm.MakeFullList;
 var AccCode: string;
begin
 if mt1.IsEmpty then AccCode:=''
               else AccCode:=mt1.FieldByName('AccCode').AsString;
 CDS.Close;
 if AccCode='' then Exit;
 SetSrvDef;
 CDS.ParamByName('AccCode').AsString:=AccCode;
 CDS.ParamByName('D1').AsDate:=Dt1.Date;
 CDS.ParamByName('D2').AsDate:=Dt2.Date;
 CDS.ParamByName('List').AsString:=GetSrvList(tbl_srv);
 CDS.Open;
 CDS.Last;
end;

procedure TNewScanEventForm.MakeUserList;
 var Code: integer;
begin
 if mt1.IsEmpty then Exit;
 if not UDS.IsEmpty then Code:=UDS.FN('Code').AsInteger else Code:=0;
 UDS.Close;
 if ch1.Checked then UDS.ParamByName('Shb').AsString:='%'
                     else UDS.ParamByName('Shb').AsString:=
                                                mt1.FieldByName('Shb').AsString;
 UDS.Open;
 UDS.Locate('Code',Code,[]);
end;
//передать счет
procedure TNewScanEventForm.N1Click(Sender: TObject);
begin
 SetUser;
end;
//изменить примечание
procedure TNewScanEventForm.N2Click(Sender: TObject);
 var Form: TSetENoteForm;
begin
 if mt1.IsEmpty then Exit;
 Form:=TSetENoteForm.Create(Application);
 Form.EventCode:=mt1.FieldByName('ECode').AsString;
 Form.ShowModal;
 if form.ModalResult=mrOk then
  Reopen;
 Form.Free;
end;

procedure TNewScanEventForm.N3Click(Sender: TObject);
 var Q: TFIBQuery;
begin
 if mt1.IsEmpty then Exit;
 if Application.MessageBox('Удалить текущее мероприятие ?','',1)<>idOk then Exit;
 Q:=GetQueryForUse(dm1.trnWr, 'delete from Events where Code=:Code');
 dm1.trnWr.StartTransaction;
 try
  Q.ParamByName('Code').AsString:=mt1.FieldByName('ECode').AsString;
  Q.ExecQuery;
 finally
  if dm1.trnWr.InTransaction then dm1.trnWr.Commit;
  Q.Free;
 end;
 Reopen;
end;
{-------------------------------------------------------------------------------
 N5Click|Отметить все
-------------------------------------------------------------------------------}
procedure TNewScanEventForm.N5Click(Sender: TObject);
var
  i: Integer;
begin
    v1.BeginUpdate;
  try
      for I := 0 to v1.DataController.GridView.ViewData.RecordCount - 1 do
       begin
         v1.DataController.GridView.ViewData.Records[i].Focused := True;
         v1.DataController.SetEditValue
                         (v1.GetColumnByFieldName('Flag').Index,True, evsValue);
         v1.DataController.Post;
       end;
   finally
     v1.EndUpdate;
  end;
end;
{-------------------------------------------------------------------------------
 N6Click|Снять отметки
-------------------------------------------------------------------------------}
procedure TNewScanEventForm.N6Click(Sender: TObject);
var
  i: Integer;
begin
    v1.BeginUpdate;
  try
      for I := 0 to v1.DataController.GridView.ViewData.RecordCount - 1 do
       begin
         v1.DataController.GridView.ViewData.Records[i].Focused := True;
         v1.DataController.SetEditValue
                        (v1.GetColumnByFieldName('Flag').Index,false, evsValue);
         v1.DataController.Post;
       end;
   finally
     v1.EndUpdate;
  end;
end;

procedure TNewScanEventForm.FormCreate(Sender: TObject);
begin
 AddButton(self);
 LoadForm(prop1,Self.Name);
 CreateTable;
 DS.Open;
 mt1.LoadFromDataSet(DS);
 v1.ApplyBestFit();
 MakeUserList;
 //создаем таблицу в памяти (услуги)
 CreateSrvTbl;
 //загружаем в таблицу памяти
 LoadSrv;

end;

{-------------------------------------------------------------------------------
LoadSrv | Загружаем список служб
-------------------------------------------------------------------------------}
procedure TNewScanEventForm.LoadSrv;
var
  DS :TFIBDataSet;
begin
  DS:=DSCreate(dm1.db1);
  //DSTemp.Close;
  DS.SelectSQL.Text:=
    'SELECT -1 Flag,CODE Srv,NAME FROM SPR$SRV WHERE State=1 ORDER BY Name';
  DS.Open;
  //DSTemp.RecordCount;
  tbl_srv.LoadFromDataSet(DS);
  DS.Free;
end;

procedure TNewScanEventForm.CreateSrvTbl;
begin
  with tbl_srv.FieldDefs do
  begin
    CreateField(tbl_srv,'Flag',ftBoolean);
    CreateField(tbl_srv,'Srv',ftString);
    CreateField(tbl_srv,'Name',ftString);
  end;
  tbl_srv.Open;
end;

function TNewScanEventForm.GetPriorCode: string;
begin
 Result:='';
 if mt1.IsEmpty then Exit;
 mt1.Prior;
 if not mt1.Bof then Result:=mt1.FieldByName('ECode').AsString;
 mt1.Next;
end;

procedure TNewScanEventForm.grd1Enter(Sender: TObject);
begin
 SetNote;
end;

procedure TNewScanEventForm.Reopen;
 var Code,PriorCode: string;
begin
 Code:=mt1.FieldByName('ECode').AsString;
 PriorCode:=GetPriorCode;
 mt1.Close;
 DS.CloseOpen(True);
 mt1.Open;
 mt1.LoadFromDataSet(DS);
 v1.ApplyBestFit();
 if not mt1.Locate('ECode', Code, []) then
  mt1.Locate('ECode', PriorCode, []);
end;

procedure TNewScanEventForm.SetUser;
 var SQL: string;
begin
 if UDS.IsEmpty or mt1.IsEmpty then Exit;
 SQL:='UPDATE N_ReestrData SET Usr='+UDS.FN('Code').AsString+
      ' WHERE acCode='#39+mt1.FieldByName('AccCode').AsString+#39;
 dm1.db1.Execute(SQL);
 Reopen;
end;

procedure TNewScanEventForm.v1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  SetNote;
  MakeUserList;
  MakeFullList;
end;

procedure TNewScanEventForm.v1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 case Key of
  VK_RETURN: SetUser;
 end;
end;

procedure TNewScanEventForm.v1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key=VK_UP) or (Key=VK_DOWN) then
  begin
    SetNote;
    MakeUserList;
    MakeFullList;
  end;
end;

procedure TNewScanEventForm.v4DblClick(Sender: TObject);
begin
   tbl_srv.Edit;
   tbl_srv.FieldByName('Flag').AsBoolean:=
                                      not tbl_srv.FieldByName('Flag').AsBoolean;
   tbl_srv.Post;
end;

procedure TNewScanEventForm.v4KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 if Key=VK_SPACE then
 begin
   tbl_srv.Edit;
   tbl_srv.FieldByName('Flag').AsBoolean:=
                                      not tbl_srv.FieldByName('Flag').AsBoolean;
   //tbl_srv.Fields[0].Value:=not tbl_srv.Fields[0].Value;
   tbl_srv.Post;
 end;
end;

procedure TNewScanEventForm.CreateTable;
begin
  with mt1.FieldDefs do
  begin
    CreateField(mt1,'Flag',ftBoolean);
    CreateField(mt1,'ECODE',ftString);
    CreateField(mt1,'ACCCODE',ftString);
    CreateField(mt1,'DACTIVE',ftDateTime);
    CreateField(mt1,'TACTIVE',ftTime);
    CreateField(mt1,'MASTER',ftString);
    CreateField(mt1,'ADDR',ftString);
    CreateField(mt1,'ADDRNAME',ftString);
    CreateField(mt1,'ETYPE',ftString);
    CreateField(mt1,'ENAME',ftString);
    CreateField(mt1,'USR',ftInteger);
    CreateField(mt1,'USRNAME',ftString);
    CreateField(mt1,'DERECT',ftSmallint);
    CreateField(mt1,'SHB',ftString);
    CreateField(mt1,'NOTE',ftString);
    CreateField(mt1,'KONTRAGENT',ftString);
    CreateField(mt1,'KONTRAGENT_NAME',ftString);
    CreateField(mt1,'SRV',ftString);
  end;
  mt1.Open;
end;

{-------------------------------------------------------------------------------
  btn_addevClick|Назначить новое мероприятие
-------------------------------------------------------------------------------}
procedure TNewScanEventForm.btn_addevClick(Sender: TObject);
begin
  MakeNewEvent;
end;

//мероприятие
procedure TNewScanEventForm.N7Click(Sender: TObject);
begin
  MakeNewEvent;
end;

//ответ
procedure TNewScanEventForm.N8Click(Sender: TObject);
begin
  CloseEvents;
end;

procedure TNewScanEventForm.MakeNewEvent;
  var SQL: string;
      Q: TFIBQuery;
      D,T: TDateTime;
      Summa: currency;
      ECode,EName, EType, ENext: string;
      Form3: TNewMPForm;
      Note: string;
      i :Integer;
      Control: word;
      ls: TStringList;
 function GetCodes :TStringList;
 var
  i :Integer;
 begin
    mt1.DisableControls;
    mt1.First;
    for I := 0 to mt1.RecordCount - 1 do
    begin
      if mt1.FieldByName('Flag').AsBoolean=true then
      begin
        Result.Add(mt1.FieldByName('AccCode').AsString);
      end;
      mt1.Next;
    end;
    mt1.EnableControls;
 end;

 function GetNewEvent: string;
  var
    Form2: TGetMListForm;
  begin
     Result:='';
     Form2:=TGetMListForm.Create(Application);
     Form2.Shablon:=GetProfileShablon;
     //Form2.Codes:=ENext;
     Form2.OpenDataSet;
     if Form2.DS.RecordCount=1 then Result:=Form2.DS.FN('Code').AsString
     else if Form2.DS.RecordCount<>0 then
      begin
       Form2.ShowModal;
       if Form2.ModalResult=mrOk then Result:=Form2.DS.FN('Code').AsString;
      end;
     if Result<>'' then EName:=Form2.DS.FN('Name').AsString;
     Form2.Free;
  end;

begin
   ls:=TStringList.Create;
   ls:=GetCodes;
   if ls.Text='' then Exit;
   ECode:=GetNewEvent;
   if ECode='' then Exit;
   //задаем параметры мероприятия
   Form3:=TNewMPForm.Create(Application);
   Form3.Caption:=EName;
   Control:=Form3.ShowModal;
   if Control=mrOk then
     begin
      D:=Form3.D.Date;
      T:=Form3.T.Time;
      Summa:=Form3.Summa.Value;
      Note:=Form3.Note.Lines.Text;
     end else begin D:=0; T:=0; Summa:=0; end;
   Form3.Free;
   if Control<>mrOk then Exit;
   //запись события
   SQL:='INSERT INTO Events(AccCode,EType,DActive,TActive,Note,Summa) '+
        'VALUES(:AccCode,:EType,:DActive,:TActive,:Note,:Summa)';
   Q:=GetQueryForUse(dm1.trnWr, SQL);
   try
    SetWaitCursor(true);
    for I := 0 to ls.Count - 1 do
    begin
      dm1.trnWr.StartTransaction;
      Q.ParamByName('AccCode').AsString:=ls[i];
      Q.ParamByName('EType').AsString:=ECode;
      Q.ParamByName('DActive').AsDate:=D;
      Q.ParamByName('TActive').AsTime:=T;
      Q.ParamByName('Note').AsString:=Note;
      Q.ParamByName('Summa').AsCurrency:=Summa;
      Q.ExecQuery;
      dm1.trnWr.Commit;
    end;
    //OpenEvents(true); //переоткрываем список событий
   finally
    SetWaitCursor(false);
    FreeQueryForUse(Q);
   end;
   MakeFullList;
   ls.Free;
end;
{-------------------------------------------------------------------------------
CloseEvents|Ответ на мероприятие
-------------------------------------------------------------------------------}
procedure TNewScanEventForm.CloseEvents;
var
  Form1 :TGetRListForm;
  EType,ENext :string;
  ls :TStringList;
 procedure CloseEvent;
  var SQL: string;
      Q: TFIBQuery;
      i :Integer;
  begin
   SQL:='UPDATE Events SET State=1,EResult=:EResult,RUsr=:RUsr WHERE Code=:Code';
   for I := 0 to Ls.Count - 1 do
   begin
     dm1.trnWr.StartTransaction;
     try
      Q:=GetQueryForUse(dm1.trnWr, SQL);
      Q.ParamByName('Code').AsString:=ls[i];
      Q.ParamByName('EResult').AsString:=EType;
      Q.ParamByName('RUsr').AsInteger:=StrToInt(UserCod);
      Q.ExecQuery;
     finally
     dm1.trnWr.Commit;
     end;
   end;
   //MakeNewEvent(GetNewEvent);
   //OpenEvents(true); //переоткрываем список событий
  end;

  function GetECode :TStringList;
   var
    i :Integer;
   begin
      mt1.DisableControls;
      mt1.First;
      for I := 0 to mt1.RecordCount - 1 do
      begin
        if mt1.FieldByName('Flag').AsBoolean=true then
        begin
          Result.Add(mt1.FieldByName('ECode').AsString);
        end;
        mt1.Next;
      end;
      mt1.EnableControls;
   end;
begin
 ls :=TStringList.Create;
 ls:=GetECode;
 if ls.Text='' then Exit;
  //выбираем ответ на событие
 Form1:=TGetRListForm.Create(Application);
 Form1.EType:=DS.FN('EType').AsString;
 Form1.ShowModal;
 EType:=Form1.DS.FN('Code').AsString;
 ENext:=Form1.DS.FN('ENext').AsString;
 Form1.Free;
 if Form1.ModalResult=mrOk then CloseEvent;
 ls.Free;
end;

end.
